﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EmployeeForm
{
    public partial class frmEmployee : UTLBASEFORM.MDICHILD
    {
        UtlEmployee utlEy = new UtlEmployee();
        DataTable data = new DataTable();
        int idEmployee = 0;
        public frmEmployee()
        {
            InitializeComponent();
        }
        private void frmEmployee_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            LoadgvEmployee();
            LoadAuth();
        }
        /// <summary>
        /// Setup menubar
        /// </summary>
        private void SetupMenuBar()
        {
           
            ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
            ShowBTN2(TextLabelEdit, ImageNameEdit);
            ShowBTN3(TextLabelDelete, ImageNameDelete);
            ShowBTN4("Hủy", null);
            ChangeButton("Insert");
            
        }
        private void ChangeButton(string state) {
            
            if (state == "Insert")
            {
                HideBTN4();
                HideBTN2();
                ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
                ShowBTN3(TextLabelDelete, ImageNameDelete);
            }
            else  if (state == "Update")
            {
              
                HideBTN1(); 
                ShowBTN2(TextLabelEdit, ImageNameEdit);
                ShowBTN4("Hủy", null);
                ShowBTN3(TextLabelDelete, ImageNameDelete);
            }

        }
        public void RefeshForm() {
            txtFullName.Text = "";
            txtUserName.Text = "";
            txtPass.Text = "";
            txtAddress.Text="";
            txtEmail.Text = "";
            txtPhone.Text = "";
            txtID.Text = "";
            txtEmail.Text = "";
            picEmployee.Image = EmployeeForm.Properties.Resources.hongphuc;

        }
        /// <summary>
        /// event add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btn1_Click(object sender, EventArgs e)
        {
            Insert();
            RefeshForm();
        }
        /// <summary>
        /// event update
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btn2_Click(object sender, EventArgs e)
        {

            Update();
            RefeshForm();
        }
        /// <summary>
        /// event delete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btn3_Click(object sender, EventArgs e)
        {
            Delete();
        }
        public override void btn4_Click(object sender, EventArgs e)
        {
            ChangeButton("Insert");
            RefeshForm();
        }
        public void LoadgvEmployee()
        {
            lvEmployee.Items.Clear();
            data = utlEy.GetEmployee();
            for (int i = 0; i < data.Rows.Count; i++)
            {   int stt= i+1;
                ListViewItem item = new ListViewItem(stt.ToString());
                lvEmployee.Items.Add(item);
                
               // item.SubItems.Add(stt.ToString());
                
                item.SubItems.Add(data.Rows[i]["FullName"].ToString());
                item.SubItems.Add(data.Rows[i]["UserName"].ToString());
                item.SubItems.Add(data.Rows[i]["AuthenticationID"].ToString());
                item.SubItems.Add(data.Rows[i]["Birthday"].ToString());
                item.SubItems.Add(data.Rows[i]["IdentityCardNumber"].ToString());
                item.SubItems.Add(data.Rows[i]["Address"].ToString());
                item.SubItems.Add(data.Rows[i]["Phone"].ToString());
                item.SubItems.Add(data.Rows[i]["Email"].ToString());
                item.SubItems.Add(data.Rows[i]["Active"].ToString());
                item.SubItems.Add(data.Rows[i]["AuthenticationID"].ToString());
                if (i % 2 == 0)
                {
                    item.BackColor = Color.AliceBlue; 
                    //return ;
                }
                else
                {
                    item.BackColor = Color.AntiqueWhite;
                    //return Brushes.Beige;
                }

            }
        }

        public void LoadAuth() {
            DataTable data = utlEy.GetAuth();
            cmbAuth.DataSource = data;
            cmbAuth.DisplayMember = data.Columns["Name"].ToString() ;
            cmbAuth.ValueMember = data.Columns["ID"].ToString();
        }
        public Employee GetInfo()
        {
            Employee ey = new Employee();
            ey.ID = idEmployee;
            ey.UserName = txtUserName.Text;
            ey.Password = txtPass.Text;
            ey.FullName = txtFullName.Text;
            ey.AuthenticationID = int.Parse(cmbAuth.SelectedValue.ToString());
            ey.Email = txtEmail.Text;
            ey.Address = txtAddress.Text;
            ey.IdentityCardNumber = txtID.Text;
            ey.Phone = txtPhone.Text;
            ey.Birthday = DateTime.Parse(dtpBirthday.Text.ToString());
            if (txtPic.Text == "")
                ey.Pic = UTLCOMMAND.UTLCommand.ImageToByteArray(picEmployee.Image);
            else {
                Bitmap pic = new Bitmap(txtPic.Text);
                ey.Pic = UTLCOMMAND.UTLCommand.ImageToByteArray(pic);
            }
            if (cmbActive.Text == "Kích hoạt")
                ey.Active = true;
            else ey.Active = false;
            return ey;
        }

        public void Insert()
        {
            Employee ey = GetInfo();
            utlEy.InsertEmployee(ey);
            LoadgvEmployee();
        }
        public void Update() {
            Employee ey = GetInfo();
            utlEy.UpdateEmployee(ey);
            LoadgvEmployee();
        }
        public void Delete() {
            for (int i = 0; i < lvEmployee.Items.Count; i++)
                if (lvEmployee.Items[i].Checked == true)
                {
                    //I show here the second field text (SubItems[1].Text) from the selected row(Items[i]) 
                    MessageBox.Show(lvEmployee.Items[i].SubItems[2].Text);
                    utlEy.DeleteEmployee(lvEmployee.Items[i].SubItems[2].Text);
                   // break;
                }
            LoadgvEmployee();
          //  
        }
        private void lvEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeButton("Update");
            ListView lv = (ListView)sender;
            int i = lv.FocusedItem.Index;
            txtUserName.Text = data.Rows[i]["UserName"].ToString();
            txtFullName.Text = data.Rows[i]["FullName"].ToString();
            txtPass.Text = data.Rows[i]["Password"].ToString();
            cmbAuth.SelectedValue = data.Rows[i]["AuthenticationID"].ToString();
            txtEmail.Text = data.Rows[i]["Email"].ToString();
            txtAddress.Text = data.Rows[i]["Address"].ToString();
            txtID.Text = data.Rows[i]["IdentityCardNumber"].ToString();
            txtPhone.Text = data.Rows[i]["Phone"].ToString();
            dtpBirthday.Value = DateTime.Parse(data.Rows[i]["Birthday"].ToString());
            Image pic = UTLCOMMAND.UTLCommand.ByteArrayToImage((Byte[])data.Rows[i]["Pic"]);
            picEmployee.Image = pic;
            txtPic.Text = "";
            if (bool.Parse(data.Rows[i]["Active"].ToString()))
            cmbActive.Text ="Kích hoạt" ;
            else cmbActive.Text = "Khóa";

            idEmployee = int.Parse(data.Rows[i]["ID"].ToString());

        }

        private void cmdOpenPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "Chọn hình!!";
            openFileDialog.InitialDirectory = @"D:\";
            openFileDialog.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.bmp)|*.bmp";
            // openFileDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            // openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtPic.Text = openFileDialog.FileName;
                Bitmap pic = new Bitmap(txtPic.Text);
                picEmployee.Image = pic;
            }
        }

      

      

      

    }
}